﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.Networking;

public class Suit : Item
{
    private string[] partNames = new string[] { "Head", "Body", "LeftArm", "RightArm", "LeftLeg", "RightLeg" };
    public List<Transform> partPrefabs = new List<Transform>();
    private List<Transform> parts = new List<Transform>();

    private void ApplyInitialPose(Skeleton skeleton)
    {
        <ApplyInitialPose>c__AnonStorey58 storey = new <ApplyInitialPose>c__AnonStorey58 {
            anim = base.GetComponent<Animator>()
        };
        if (storey.anim != null)
        {
            storey.anim.enabled = false;
        }
        skeleton.ApplyInitialPose();
        if (storey.anim != null)
        {
            skeleton.StartWaitForEndOfFrame(new Action(storey.<>m__90));
        }
    }

    public override void Equip(Transform modelParent)
    {
        base.Equip(modelParent);
        this.ApplyInitialPose(modelParent.GetComponent<Skeleton>());
        if (base.model != null)
        {
            base.model.SetInstanceActive(false);
        }
        if (this.partPrefabs.Count == 0)
        {
            <Equip>c__AnonStorey56 storey = new <Equip>c__AnonStorey56 {
                modelFileName = this.GetModelFileName()
            };
            foreach (string str in this.partNames.Select<string, string>(new Func<string, string>(storey.<>m__8D)).ToArray<string>())
            {
                if (AssetManager.HasModel(str))
                {
                    GameObject obj2 = AssetManager.LoadModel(str);
                    this.partPrefabs.Add(obj2.transform);
                }
            }
        }
        <Equip>c__AnonStorey57 storey2 = new <Equip>c__AnonStorey57();
        Transform[] componentsInChildren = modelParent.GetComponentsInChildren<Transform>();
        for (int i = 0; i < componentsInChildren.Length; i++)
        {
            storey2.parent = componentsInChildren[i];
            Transform comp = this.partPrefabs.Find(new Predicate<Transform>(storey2.<>m__8E));
            if (comp != null)
            {
                Transform tsm = this.parts.Find(new Predicate<Transform>(storey2.<>m__8F));
                if (tsm == null)
                {
                    tsm = comp.CloneGameObjectAsChild<Transform>(base.transform, true);
                }
                tsm.ResetTransform();
                tsm.gameObject.SetActive(true);
                tsm.parent = storey2.parent;
                tsm.name = storey2.parent.name;
                this.parts.Add(tsm);
            }
        }
    }

    public override void OnDeserialize(NetworkReader reader, bool initialState)
    {
        base.OnDeserialize(reader, initialState);
    }

    public override bool OnSerialize(NetworkWriter writer, bool forceAll)
    {
        bool flag;
        bool flag2 = base.OnSerialize(writer, forceAll);
        return (flag | flag2);
    }

    protected override void Start()
    {
        base.Start();
    }

    public override void Unequip()
    {
        base.Unequip();
        if (base.model != null)
        {
            base.model.SetInstanceActive(true);
        }
        foreach (Transform transform in this.parts)
        {
            transform.parent = base.transform;
            transform.gameObject.SetActive(false);
        }
    }

    private void UNetVersion()
    {
    }

    [CompilerGenerated]
    private sealed class <ApplyInitialPose>c__AnonStorey58
    {
        internal Animator anim;

        internal void <>m__90()
        {
            this.anim.enabled = true;
        }
    }

    [CompilerGenerated]
    private sealed class <Equip>c__AnonStorey56
    {
        internal string modelFileName;

        internal string <>m__8D(string x)
        {
            return PathUtility.ChangeFileName(this.modelFileName, x);
        }
    }

    [CompilerGenerated]
    private sealed class <Equip>c__AnonStorey57
    {
        internal Transform parent;

        internal bool <>m__8E(Transform x)
        {
            return (Path.GetFileNameWithoutExtension(x.name).ToLower() == this.parent.name.ToLower());
        }

        internal bool <>m__8F(Transform x)
        {
            return (x.name == this.parent.name);
        }
    }
}

